import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_vant_ui/flutter_vant_ui.dart';
import 'package:vant/src/app.dart';
import '../routes.dart';
import '../widgets/widgets.dart';

typedef VanDocDemoRoute = Map<String, dynamic>;

class VanDocDemoHomeSection extends StatelessWidget {
  const VanDocDemoHomeSection({super.key});

  @override
  Widget build(BuildContext context) {
    final Future<List<dynamic>> routesFuture = rootBundle.loadString("assets/json/routes.json").then((value) => json.decode(value));
    return Scaffold(
      body: SingleChildScrollView(
        child: SafeArea(
          bottom: false,
          child: Padding(
            padding: const EdgeInsets.all(20).copyWith(top: 46, bottom: 40),
            child: VanProvider(
              provide: VanCellGroupStyle(
                radius: const BorderRadius.all(Radius.circular(99)),
                cellStyle: VanCellStyle(
                  fontSize: 14,
                  textColor: kVanDocTextColor3.get(context),
                  background: VanBgColor(kVanDocBackground2.get(context)),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      const SizedBox(width: 16),
                      const Image(image: AssetImage("assets/logo.png"), width: 32),
                      const SizedBox(width: 16),
                      const Image(image: AssetImage("assets/images/flutter.png"), height: 28, color: Colors.blue),
                      const SizedBox(width: 16),
                      Text("Vant 4", style: TextStyle(fontSize: 32, color: kVanDocTextColor2.get(context), fontWeight: FontWeight.w400)),
                    ],
                  ),
                  // const SizedBox(width: 16),
                  Padding(
                    padding: const EdgeInsets.only(left: 16, top: 10, bottom: 16),
                    child: Text(
                      "轻量、可定制的移动端 Flutter 组件库",
                      style: TextStyle(color: kVanDocTextColor4.get(context), fontSize: 14, height: 1.6),
                    ),
                  ),
                  FutureBuilder(
                      future: routesFuture,
                      initialData: const [],
                      builder: (context, snapshot) {
                        final groups = (snapshot.data ?? []);
                        return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: groups.map((g) {
                              final routes = (g['routes'] as List? ?? []);
                              return VanDocDemoHomeNav(
                                  title: g['title'] ?? "",
                                  children: routes.map((r) {
                                    return VanDocDemoHomeBlock(title: r["title"], route: r["route"]);
                                  }).toList());
                            }).toList());
                      }),
                ],
              ),
            ),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: kVanDocBackground3.get(context),
        child: Icon(
          Brightness.light != Theme.of(context).brightness ? Icons.nightlight : Icons.light_mode,
          color: kVanDocTextColor2.get(context),
        ),
        onPressed: () => VanDocApp.switchBrightness(context),
      ),
    );
  }
}

class VanDocDemoHomeNav extends StatelessWidget {
  final String title;
  final List<Widget>? children;

  const VanDocDemoHomeNav({super.key, required this.title, this.children});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 16, top: 24, bottom: 8),
          child: Text(title, style: TextStyle(color: kVanDocTextColor4.get(context), fontSize: 14, height: 16 / 14)),
        ),
        Wrap(runSpacing: 12, children: children ?? [])
      ],
    );
  }
}

class VanDocDemoHomeBlock extends StatelessWidget {
  final String title;
  final String route;

  const VanDocDemoHomeBlock({super.key, required this.title, required this.route});

  @override
  Widget build(BuildContext context) {
    final tag = routes.containsKey("/$route") ? "" : "(暂未开发)";
    Widget widget = Container(
      decoration: BoxDecoration(
        color: kVanDocBackground3.toColor(context),
        borderRadius: const BorderRadius.all(Radius.circular(99)),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            Future.delayed(const Duration(milliseconds: 350)).then((value) {
              if (isProduct) {
                Navigator.of(context).pushNamed("/$route");
              } else {
                Navigator.of(context).pushReplacementNamed("/$route");
              }
            });
          },
          borderRadius: const BorderRadius.all(Radius.circular(99)),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
              Text(
                "$route $title $tag",
                style: TextStyle(
                  fontSize: 14,
                  height: 40 / 14,
                  color: kVanDocTextColor3.toColor(context),
                  fontWeight: FontWeight.w600,
                ),
              ),
              Icon(
                Icons.keyboard_arrow_right_sharp,
                size: 14,
                color: kVanDocTextColor3.toColor(context),
                weight: 600,
              )
            ]),
          ),
        ),
      ),
    );
    if (isProduct) return widget;
    if (routes.containsKey("/$route")) return widget;
    return Visibility(visible: false, child: widget);
  }
}
